package edu.umd.essic.maskbuilder;

import java.awt.*;

public class FileContentsDlg extends javax.swing.JDialog {
    public boolean OnOK, new_dym, interpol, doubles, bilinear, cubicspl, averaging, adjustmsk, missval;
    public int nlon,nlat,funcID,nlevel,missingID,MVM_flag;
    public float minval,maxval,startdate,enddate,xlon_0, xlon_nlon, ylat_0,ylat_nlat,zlevel_0,zlevel_nlevel;
    private float start_init,end_init;
    public int nlon_new, nlat_new; 
    
    String[] Method = {"Cubic splines","Bilinear","Averaging"};
    String[] MVMethod = {"Do nothing","Replace with 0","Interpolate"};
   
    /** Creates new form constGridDialog */
    public FileContentsDlg(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        for (int n=0; n<Method.length; n++)
            Method_Combo.addItem(Method[n]);
        for (int n=0; n<MVMethod.length; n++)
            MVMethod_Combo.addItem(MVMethod[n]);
    }
    
    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public void getReturnStatus() {
        if (OnOK){
            nlon_new  = Integer.parseInt(Nlon_text.getText());
            nlat_new  = Integer.parseInt(Nlat_text.getText());
            startdate = Float.parseFloat(StartDate_text.getText());
            enddate   = Float.parseFloat(  EndDate_text.getText());
            new_dym   = Format_CheckBox.isSelected();
            interpol  = !NoInterpol_CheckBox.isSelected();
            //bilinear  = Bilinear_CheckBox.isSelected();
            cubicspl = false; 
            bilinear = false;
            averaging = false;
            if (Method_Combo.getSelectedIndex() == 0)
                cubicspl  = true;
            if (Method_Combo.getSelectedIndex() == 1)
                bilinear = true;
            if (Method_Combo.getSelectedIndex() == 2)
                averaging = true;
            //averaging = Average_CheckBox.isSelected();
            adjustmsk = AdjustMask_CheckBox.isSelected();
            doubles   = Doubles_CheckBox.isSelected();
            missval   = Missing_CheckBox.isSelected();
            missingID = -9999;
            if (missval){
                missingID = Integer.parseInt(MissingID_text.getText());
                MVM_flag = MVMethod_Combo.getSelectedIndex();
            }
        }
    }
    
    private boolean CheckValues() {
        boolean answer = true;
        try {
            nlon_new  = Integer.parseInt(Nlon_text.getText());
            nlat_new  = Integer.parseInt(Nlat_text.getText());
            startdate = Float.parseFloat(StartDate_text.getText());
            enddate   = Float.parseFloat(EndDate_text.getText());
        } catch (NumberFormatException e){ return false;}
        if ((startdate <=0)||(enddate <=0)|| (startdate>enddate)||
            (nlon_new <= 0)||(nlat_new <= 0)||(startdate<start_init)||(enddate>end_init)) 
            answer = false;       
        return answer;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanelFROM = new javax.swing.JPanel();
        FileID_label = new javax.swing.JLabel();
        FileID_text = new javax.swing.JTextField();
        StretchingID_label = new javax.swing.JLabel();
        StretchingID_text = new javax.swing.JTextField();
        MINVAL_label = new javax.swing.JLabel();
        MINVAL_text = new javax.swing.JTextField();
        MAXVAL_label = new javax.swing.JLabel();
        MAXVAL_text = new javax.swing.JTextField();
        NLON_label = new javax.swing.JLabel();
        NLON_text = new javax.swing.JTextField();
        NLAT_label = new javax.swing.JLabel();
        NLAT_text = new javax.swing.JTextField();
        NLEVEL_label = new javax.swing.JLabel();
        NLEVEL_text = new javax.swing.JTextField();
        ZLEVEL_label = new javax.swing.JLabel();
        ZLEVEL_text = new javax.swing.JTextField();
        StartDate_label = new javax.swing.JLabel();
        StartDate_text = new javax.swing.JTextField();
        EndDate_label = new javax.swing.JLabel();
        EndDate_text = new javax.swing.JTextField();
        XLON_label = new javax.swing.JLabel();
        XLON_text = new javax.swing.JTextField();
        YLAT_label = new javax.swing.JLabel();
        YLAT_text = new javax.swing.JTextField();
        jPanelTO = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        Method_Combo = new javax.swing.JComboBox();
        AdjustMask_CheckBox = new javax.swing.JCheckBox();
        jPanel5 = new javax.swing.JPanel();
        Nlon_label = new javax.swing.JLabel();
        Nlon_text = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        Missing_CheckBox = new javax.swing.JCheckBox();
        MVMethod_Combo = new javax.swing.JComboBox();
        jPanel9 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        MissingID_text = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        Nlat_label = new javax.swing.JLabel();
        Nlat_text = new javax.swing.JTextField();
        jPanel10 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        Format_CheckBox = new javax.swing.JCheckBox();
        Doubles_CheckBox = new javax.swing.JCheckBox();
        NoInterpol_CheckBox = new javax.swing.JCheckBox();
        jPanel8 = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.GridLayout(2, 0));

        setTitle("Dym File Data");
        setModal(true);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                OnShowDlg(evt);
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanelFROM.setLayout(new java.awt.GridLayout(6, 4, 5, 6));

        jPanelFROM.setBorder(new javax.swing.border.TitledBorder("File header data"));
        jPanelFROM.setMinimumSize(new java.awt.Dimension(350, 220));
        jPanelFROM.setPreferredSize(new java.awt.Dimension(400, 220));
        FileID_label.setText("File ID");
        FileID_label.setPreferredSize(new java.awt.Dimension(30, 20));
        jPanelFROM.add(FileID_label);

        FileID_text.setEditable(false);
        FileID_text.setText("DYM2");
        FileID_text.setPreferredSize(new java.awt.Dimension(33, 22));
        jPanelFROM.add(FileID_text);

        StretchingID_label.setText("Stretching ID");
        StretchingID_label.setPreferredSize(new java.awt.Dimension(63, 20));
        jPanelFROM.add(StretchingID_label);

        StretchingID_text.setEditable(false);
        StretchingID_text.setText(null);
        StretchingID_text.setPreferredSize(new java.awt.Dimension(30, 22));
        jPanelFROM.add(StretchingID_text);

        MINVAL_label.setText("MINVAL");
        MINVAL_label.setPreferredSize(new java.awt.Dimension(37, 20));
        jPanelFROM.add(MINVAL_label);

        MINVAL_text.setEditable(false);
        MINVAL_text.setText(null);
        MINVAL_text.setMinimumSize(new java.awt.Dimension(6, 22));
        MINVAL_text.setPreferredSize(new java.awt.Dimension(30, 20));
        jPanelFROM.add(MINVAL_text);

        MAXVAL_label.setText("MAXVAL");
        MAXVAL_label.setPreferredSize(new java.awt.Dimension(39, 20));
        jPanelFROM.add(MAXVAL_label);

        MAXVAL_text.setEditable(false);
        MAXVAL_text.setText(null);
        MAXVAL_text.setPreferredSize(new java.awt.Dimension(30, 22));
        jPanelFROM.add(MAXVAL_text);

        NLON_label.setText("NLON");
        NLON_label.setPreferredSize(new java.awt.Dimension(27, 20));
        jPanelFROM.add(NLON_label);

        NLON_text.setEditable(false);
        NLON_text.setText(null);
        NLON_text.setPreferredSize(new java.awt.Dimension(30, 22));
        jPanelFROM.add(NLON_text);

        NLAT_label.setText("NLAT");
        NLAT_label.setPreferredSize(new java.awt.Dimension(25, 20));
        jPanelFROM.add(NLAT_label);

        NLAT_text.setEditable(false);
        NLAT_text.setText(null);
        NLAT_text.setPreferredSize(new java.awt.Dimension(30, 22));
        jPanelFROM.add(NLAT_text);

        NLEVEL_label.setText("NLEVEL");
        NLEVEL_label.setPreferredSize(new java.awt.Dimension(35, 20));
        jPanelFROM.add(NLEVEL_label);

        NLEVEL_text.setEditable(false);
        NLEVEL_text.setText(null);
        NLEVEL_text.setPreferredSize(new java.awt.Dimension(30, 22));
        jPanelFROM.add(NLEVEL_text);

        ZLEVEL_label.setText("ZLEVEL");
        ZLEVEL_label.setPreferredSize(new java.awt.Dimension(34, 20));
        jPanelFROM.add(ZLEVEL_label);

        ZLEVEL_text.setEditable(false);
        ZLEVEL_text.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        ZLEVEL_text.setText(null);
        ZLEVEL_text.setPreferredSize(new java.awt.Dimension(30, 22));
        jPanelFROM.add(ZLEVEL_text);

        StartDate_label.setText("Start Date");
        StartDate_label.setPreferredSize(new java.awt.Dimension(50, 20));
        jPanelFROM.add(StartDate_label);

        StartDate_text.setText(null);
        StartDate_text.setPreferredSize(new java.awt.Dimension(30, 22));
        jPanelFROM.add(StartDate_text);

        EndDate_label.setText("End Date");
        EndDate_label.setPreferredSize(new java.awt.Dimension(44, 20));
        jPanelFROM.add(EndDate_label);

        EndDate_text.setText(null);
        EndDate_text.setPreferredSize(new java.awt.Dimension(30, 22));
        jPanelFROM.add(EndDate_text);

        XLON_label.setText("XLON");
        XLON_label.setPreferredSize(new java.awt.Dimension(26, 20));
        jPanelFROM.add(XLON_label);

        XLON_text.setEditable(false);
        XLON_text.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        XLON_text.setText(null);
        XLON_text.setPreferredSize(new java.awt.Dimension(30, 22));
        jPanelFROM.add(XLON_text);

        YLAT_label.setText("YLAT");
        YLAT_label.setPreferredSize(new java.awt.Dimension(24, 20));
        jPanelFROM.add(YLAT_label);

        YLAT_text.setEditable(false);
        YLAT_text.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        YLAT_text.setText(null);
        YLAT_text.setPreferredSize(new java.awt.Dimension(30, 22));
        jPanelFROM.add(YLAT_text);

        getContentPane().add(jPanelFROM);

        jPanelTO.setLayout(new java.awt.GridLayout(2, 1, 0, 2));

        jPanelTO.setMinimumSize(new java.awt.Dimension(350, 170));
        jPanelTO.setPreferredSize(new java.awt.Dimension(400, 220));
        jPanel1.setLayout(new java.awt.GridLayout(3, 0, 0, 10));

        jPanel1.setBorder(new javax.swing.border.TitledBorder("Interpolation"));
        jPanel1.setPreferredSize(new java.awt.Dimension(380, 100));
        jPanel4.setLayout(new java.awt.GridLayout(1, 4, 5, 5));

        jPanel4.setAlignmentX(0.0F);
        jPanel4.setAlignmentY(0.0F);
        jPanel4.setPreferredSize(new java.awt.Dimension(360, 60));
        jLabel1.setText("Choose method:");
        jLabel1.setAlignmentX(0.5F);
        jPanel4.add(jLabel1);

        Method_Combo.setPreferredSize(new java.awt.Dimension(29, 20));
        jPanel4.add(Method_Combo);

        AdjustMask_CheckBox.setText("Adjust mask");
        AdjustMask_CheckBox.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jPanel4.add(AdjustMask_CheckBox);

        jPanel5.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

        jPanel5.setPreferredSize(new java.awt.Dimension(56, 24));
        Nlon_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Nlon_label.setText("Nlon");
        jPanel5.add(Nlon_label);

        Nlon_text.setPreferredSize(new java.awt.Dimension(20, 20));
        jPanel5.add(Nlon_text);

        jPanel4.add(jPanel5);

        jPanel1.add(jPanel4);

        jPanel6.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

        Missing_CheckBox.setLabel("Miss. values");
        Missing_CheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Missing_CheckBoxActionPerformed(evt);
            }
        });

        jPanel6.add(Missing_CheckBox);

        MVMethod_Combo.setPreferredSize(new java.awt.Dimension(29, 20));
        MVMethod_Combo.setEnabled(false);
        jPanel6.add(MVMethod_Combo);

        jPanel9.setLayout(new java.awt.GridLayout());

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("ID:  ");
        jLabel2.setPreferredSize(new java.awt.Dimension(26, 14));
        jPanel9.add(jLabel2);

        MissingID_text.setEditable(false);
        MissingID_text.setText("-9999");
        MissingID_text.setPreferredSize(new java.awt.Dimension(36, 20));
        jPanel9.add(MissingID_text);

        jPanel6.add(jPanel9);

        jPanel2.setLayout(new java.awt.GridLayout(1, 0, 5, 5));

        jPanel2.setPreferredSize(new java.awt.Dimension(54, 24));
        Nlat_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Nlat_label.setText("Nlat");
        jPanel2.add(Nlat_label);

        Nlat_text.setPreferredSize(new java.awt.Dimension(20, 20));
        jPanel2.add(Nlat_text);

        jPanel6.add(jPanel2);

        jPanel1.add(jPanel6);

        jLabel11.setFont(new java.awt.Font("MS Sans Serif", 2, 10));
        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel11.setText("(Note: if you choose another resolution regular grid will be created)");
        jLabel11.setAlignmentY(0.0F);
        jLabel11.setPreferredSize(new java.awt.Dimension(380, 24));
        jLabel11.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabel11.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
        jPanel10.add(jLabel11);

        jPanel1.add(jPanel10);

        jPanelTO.add(jPanel1);
        jPanel1.getAccessibleContext().setAccessibleName("Interpolation");

        jPanel3.setLayout(new java.awt.GridLayout(2, 1, 0, 10));

        jPanel3.setPreferredSize(new java.awt.Dimension(380, 102));
        jPanel7.setLayout(new java.awt.GridLayout(1, 3));

        jPanel7.setBorder(new javax.swing.border.TitledBorder("Output"));
        Format_CheckBox.setSelected(true);
        Format_CheckBox.setText("DYM2 format");
        Format_CheckBox.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        Format_CheckBox.setPreferredSize(new java.awt.Dimension(120, 20));
        jPanel7.add(Format_CheckBox);

        Doubles_CheckBox.setText("Double precision");
        Doubles_CheckBox.setPreferredSize(new java.awt.Dimension(120, 20));
        jPanel7.add(Doubles_CheckBox);

        NoInterpol_CheckBox.setLabel("Do not interpolate");
        NoInterpol_CheckBox.setPreferredSize(new java.awt.Dimension(120, 20));
        jPanel7.add(NoInterpol_CheckBox);

        jPanel3.add(jPanel7);

        jPanel8.setLayout(new java.awt.GridLayout(1, 2, 1, 0));

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        jPanel8.add(okButton);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jPanel8.add(cancelButton);

        jPanel3.add(jPanel8);

        jPanelTO.add(jPanel3);

        getContentPane().add(jPanelTO);

        pack();
    }//GEN-END:initComponents

    private void Missing_CheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Missing_CheckBoxActionPerformed
        missval = Missing_CheckBox.isSelected();
        if (missval) {
            MissingID_text.setEditable(true);
            MVMethod_Combo.setEnabled(true);
        }
        else {
            MVMethod_Combo.setEnabled(false);
            MissingID_text.setEditable(false);
        }
    }//GEN-LAST:event_Missing_CheckBoxActionPerformed

    private void OnShowDlg(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_OnShowDlg
        getRootPane().setDefaultButton(okButton);
        String str=null;
        if (new_dym){
            str = String.valueOf(funcID);
            StretchingID_text.setText(str);
            str = String.valueOf(minval);
            MINVAL_text.setText(str);
            str = String.valueOf(maxval);
            MAXVAL_text.setText(str);
            str = String.valueOf(nlon);
            NLON_text.setText(str);
            str = String.valueOf(nlat);
            NLAT_text.setText(str);
            str = String.valueOf(nlevel);
            NLEVEL_text.setText(str);
            str = String.valueOf(startdate);
            StartDate_text.setText(str);
            str = String.valueOf(enddate);
            EndDate_text.setText(str);
            str = "("+String.valueOf((int)zlevel_0)+","+String.valueOf((int)zlevel_nlevel)+")" ;
            ZLEVEL_text.setText(str);
            str = "("+String.valueOf(xlon_0) + ","+String.valueOf(xlon_nlon)+")";
            XLON_text.setText(str);
            str = "("+String.valueOf(ylat_0) + ","+String.valueOf(ylat_nlat)+")";
            YLAT_text.setText(str);
            } else{
                str = String.valueOf(nlon);
                NLON_text.setText(str);
                str = String.valueOf(nlat);
                NLAT_text.setText(str);
                str = String.valueOf(nlevel);
                NLEVEL_text.setText(str);
                str = String.valueOf(startdate);
                StartDate_text.setText(str);
                str = String.valueOf(enddate);
                EndDate_text.setText(str);
                str = "("+String.valueOf((int)zlevel_0)+","+String.valueOf((int)zlevel_nlevel)+")" ;
                ZLEVEL_text.setText(str);
                str = "("+String.valueOf(xlon_0) + ","+String.valueOf(xlon_nlon)+")";
                XLON_text.setText(str);
                str = "("+String.valueOf(ylat_0) + ","+String.valueOf(ylat_nlat)+")";
                YLAT_text.setText(str);
            }
        str = String.valueOf(nlon_new);
        Nlon_text.setText(str);
        str = String.valueOf(nlat_new);
        Nlat_text.setText(str);
        start_init = startdate;
        end_init   = enddate;
    }//GEN-LAST:event_OnShowDlg
    
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if (!CheckValues()){
            javax.swing.JOptionPane.showMessageDialog(this,"Wrong value!","Error",javax.swing.JOptionPane.ERROR_MESSAGE);
        } else {
            OnOK = true;
            doClose();
        }
    }//GEN-LAST:event_okButtonActionPerformed
    
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        doClose();
        OnOK = false;
    }//GEN-LAST:event_cancelButtonActionPerformed
    
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose();
    }//GEN-LAST:event_closeDialog
    
    public void doClose() {
        setVisible(false);
        dispose();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox AdjustMask_CheckBox;
    private javax.swing.JCheckBox Doubles_CheckBox;
    private javax.swing.JLabel EndDate_label;
    private javax.swing.JTextField EndDate_text;
    private javax.swing.JLabel FileID_label;
    private javax.swing.JTextField FileID_text;
    private javax.swing.JCheckBox Format_CheckBox;
    private javax.swing.JLabel MAXVAL_label;
    private javax.swing.JTextField MAXVAL_text;
    private javax.swing.JLabel MINVAL_label;
    private javax.swing.JTextField MINVAL_text;
    private javax.swing.JComboBox MVMethod_Combo;
    private javax.swing.JComboBox Method_Combo;
    private javax.swing.JTextField MissingID_text;
    private javax.swing.JCheckBox Missing_CheckBox;
    private javax.swing.JLabel NLAT_label;
    private javax.swing.JTextField NLAT_text;
    private javax.swing.JLabel NLEVEL_label;
    private javax.swing.JTextField NLEVEL_text;
    private javax.swing.JLabel NLON_label;
    private javax.swing.JTextField NLON_text;
    private javax.swing.JLabel Nlat_label;
    private javax.swing.JTextField Nlat_text;
    private javax.swing.JLabel Nlon_label;
    private javax.swing.JTextField Nlon_text;
    private javax.swing.JCheckBox NoInterpol_CheckBox;
    private javax.swing.JLabel StartDate_label;
    private javax.swing.JTextField StartDate_text;
    private javax.swing.JLabel StretchingID_label;
    private javax.swing.JTextField StretchingID_text;
    private javax.swing.JLabel XLON_label;
    private javax.swing.JTextField XLON_text;
    private javax.swing.JLabel YLAT_label;
    private javax.swing.JTextField YLAT_text;
    private javax.swing.JLabel ZLEVEL_label;
    private javax.swing.JTextField ZLEVEL_text;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JPanel jPanelFROM;
    private javax.swing.JPanel jPanelTO;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
}
